//
//  CYSearchViewController.m
//  cyydt
//
//  Created by chu on 2019/1/31.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYSearchViewController.h"

#import "CYSearch_HotTableViewCell.h"
#import "CYSearch_HistoryTableViewCell.h"

#import "CYSearchProjectTableViewCell.h"
#import "CYSearchArticleTableViewCell.h"
#import "CYSearchVideoTableViewCell.h"
#import "CYSearchWenDaTableViewCell.h"

#import "CYArcitleDetailsViewController.h"
#import "CYHomeVideoDetailsViewController.h"
#import "CYProjectDetailViewController.h"
#import "CYWenDaDetailViewController.h"

@interface CYSearchViewController ()<UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate, SGTopScrollMenuDelegate>
{
    UITextField *_textField;
    
    NSString *_type;
    NSString *_searchText;
    
}
@property (nonatomic, strong) UIView *titleView;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *hotArr;

@property (nonatomic, strong) NSMutableArray *historyArr;

@property (nonatomic, strong) NSMutableArray *resultArr;

@property (nonatomic, strong) SGTopScrollMenu *topMenu;

@property (nonatomic, strong) UIView *tableHeaderView;

@property (nonatomic, assign) NSInteger count;

@property (nonatomic, assign) BOOL isSearch;

@end

@implementation CYSearchViewController

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:nil];
    
    _type = @"3";
    self.isSearch = NO;
    NSUserDefaults *us = [NSUserDefaults standardUserDefaults];
    if ([us objectForKey:@"searchHistory"]) {
        NSArray *data = [us objectForKey:@"searchHistory"];
        [self.historyArr removeAllObjects];
        [self.historyArr addObjectsFromArray:data];
    }
    
    self.navigationItem.titleView = self.titleView;
    [self.view addSubview:self.tableView];
    [self getHot];
    self.tableView.estimatedRowHeight = 44;
    if (@available(iOS 11.0, *)) {
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

-(void)textChange{
    if (_textField.text.length == 0)
    {
        self.isSearch = NO;
        
        [self.tableHeaderView removeFromSuperview];
        self.tableView.frame = CGRectMake(0, 0, kWindowW, kWindowH - Height_NavBar);
        [self.resultArr removeAllObjects];
        [self.tableView reloadData];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.isSearch) {
        return self.resultArr.count;
    }else{
        if (self.historyArr.count > 0) {
            return 2;
        }else{
            return 1;
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.isSearch) {
        return self.resultArr.count == 0 ? 0 : 1;
    }
    if (self.historyArr.count > 0) {
        if (section == 0) {
            return self.historyArr.count;
        }else{
            return self.hotArr.count;
        }
    }else{
        return self.hotArr.count;
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isSearch) {
        if ([_type isEqualToString:@"3"]) {
            //项目
            CYSearchProjectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYSearchProjectTableViewCell"];
            if (!cell) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CYSearchProjectTableViewCell" owner:nil options:nil][0];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.resultArr[indexPath.section];
            return cell;
        }else if ([_type isEqualToString:@"4"]){
            //问答
            CYSearchWenDaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYSearchWenDaTableViewCell"];
            if (!cell) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CYSearchWenDaTableViewCell" owner:nil options:nil][0];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.resultArr[indexPath.section];
            return cell;
        }else if ([_type isEqualToString:@"28"]){
            //视频
            CYSearchVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYSearchVideoTableViewCell"];
            if (!cell) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CYSearchVideoTableViewCell" owner:nil options:nil][0];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.resultArr[indexPath.section];
            return cell;
        }else{
            //资讯
            CYSearchArticleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYSearchArticleTableViewCell"];
            if (!cell) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CYSearchArticleTableViewCell" owner:nil options:nil][0];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.resultArr[indexPath.section];
            return cell;
        }
    }else{
        if (self.historyArr.count > 0) {
            if (indexPath.section == 0) {
                CYSearch_HistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYSearch_HistoryTableViewCell"];
                if (!cell) {
                    cell = [[NSBundle mainBundle] loadNibNamed:@"CYSearch_HistoryTableViewCell" owner:nil options:nil][0];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.titleLabel.text = self.historyArr[indexPath.row];
                cell.block = ^{
                    [self delHistoryWithTag:indexPath.row];
                };
                return cell;
            }else{
                CYSearch_HotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYSearch_HotTableViewCell"];
                if (!cell) {
                    cell = [[NSBundle mainBundle] loadNibNamed:@"CYSearch_HotTableViewCell" owner:nil options:nil][0];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
                cell.numLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
                cell.titleLabel.text = self.hotArr[indexPath.row];
                if (indexPath.row == 0) {
                    cell.numLabel.backgroundColor = RGBOF(0xF43636);
                }else if (indexPath.row == 1){
                    cell.numLabel.backgroundColor = RGBOF(0xFF6D1E);
                }else if (indexPath.row == 2){
                    cell.numLabel.backgroundColor = RGBOF(0xF89660);
                }else{
                    cell.numLabel.backgroundColor = RGBOF(0xB7B7B7);
                }
                
                return cell;
            }
        }else{
            CYSearch_HotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYSearch_HotTableViewCell"];
            if (!cell) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CYSearch_HotTableViewCell" owner:nil options:nil][0];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            cell.numLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
            cell.titleLabel.text = self.hotArr[indexPath.row];
            if (indexPath.row == 0) {
                cell.numLabel.backgroundColor = RGBOF(0xF43636);
            }else if (indexPath.row == 1){
                cell.numLabel.backgroundColor = RGBOF(0xFF6D1E);
            }else if (indexPath.row == 2){
                cell.numLabel.backgroundColor = RGBOF(0xF89660);
            }else{
                cell.numLabel.backgroundColor = RGBOF(0xB7B7B7);
            }
            
            return cell;
        }
    }
    
}

- (void)delHistoryWithTag:(NSInteger)tag{
    NSUserDefaults *us = [NSUserDefaults standardUserDefaults];
    if ([us objectForKey:@"searchHistory"]) {
        NSArray *data = [us objectForKey:@"searchHistory"];
        NSMutableArray *muData = [NSMutableArray arrayWithArray:data];
        [muData removeObjectAtIndex:tag];
        [us setObject:muData forKey:@"searchHistory"];
        [self.historyArr removeObjectAtIndex:tag];
        [self.tableView reloadData];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isSearch) {
        if ([_type isEqualToString:@"3"]) {
            //项目
            return 130;
        }else if ([_type isEqualToString:@"4"]){
            //问答
            return UITableViewAutomaticDimension;
        }else if ([_type isEqualToString:@"28"]){
            //视频
            return 100;
        }else{
            //资讯
            return UITableViewAutomaticDimension;
        }
    }
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (self.isSearch) {
        if (section == 0) {
            return 30;
        }
        return 0.0001f;
    }
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    [view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if (self.resultArr.count == 0) {
        if (self.historyArr.count > 0) {
            if (section == 0) {
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 100, 20)];
                label.textColor = RGBOF(0x999999);
                label.font = [UIFont systemFontOfSize:12];
                label.text = @"历史记录";
                [view addSubview:label];
                
                UIButton *more = [UIButton buttonWithType:UIButtonTypeCustom];
                more.frame = CGRectMake(kWindowW - 29, 13, 14, 18);
                [more setImage:[UIImage imageNamed:@"ss_del"] forState:UIControlStateNormal];
                [view addSubview:more];
                [more addTarget:self action:@selector(delHistory:) forControlEvents:UIControlEventTouchUpInside];
                UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39, kWindowW, 1)];
                lineView.backgroundColor = RGBOF(0xEDEDED);
                [view addSubview:lineView];
            }else{
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 100, 20)];
                label.textColor = RGBOF(0x999999);
                label.font = [UIFont systemFontOfSize:12];
                label.text = @"热门搜索";
                [view addSubview:label];
                
                UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39, kWindowW, 1)];
                lineView.backgroundColor = RGBOF(0xEDEDED);
                [view addSubview:lineView];
            }
        }else{
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 100, 20)];
            label.textColor = RGBOF(0x999999);
            label.font = [UIFont systemFontOfSize:12];
            label.text = @"热门搜索";
            [view addSubview:label];
            
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39, kWindowW, 1)];
            lineView.backgroundColor = RGBOF(0xEDEDED);
            [view addSubview:lineView];
        }
    }else{
        if (section == 0) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 30)];
            label.backgroundColor = BackGroundColor;
            label.textColor = RGBOF(0xA5A9B1);
            label.font = [UIFont systemFontOfSize:11];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = [NSString stringWithFormat:@"创业搜索为您找到相关结果约%ld个",self.count];
            [view addSubview:label];
        }
    }
    return view;
}

- (void)delHistory:(UIButton *)sender{
    NSUserDefaults *us = [NSUserDefaults standardUserDefaults];
    if ([us objectForKey:@"searchHistory"]) {
        [us removeObjectForKey:@"searchHistory"];
        [self.historyArr removeAllObjects];
        [self.tableView reloadData];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.isSearch) {
        return 10;
    }
    return 0.0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isSearch) {
        CYProjectModel *model = self.resultArr[indexPath.section];
        
        if ([_type integerValue] == 7) {
            CYArcitleDetailsViewController *detail = [[CYArcitleDetailsViewController alloc] init];
            detail.articleId = [model.ID stringValue];
            [[AppDelegate sharedAppDelegate] pushViewController:detail];
        }else if ([_type integerValue] == 3){
            CYProjectDetailViewController *detail = [[CYProjectDetailViewController alloc] init];
            detail.model = model;
            [[AppDelegate sharedAppDelegate] pushViewController:detail];
        }else if ([_type integerValue] == 4){
            CYWenDaDetailViewController *detail = [[CYWenDaDetailViewController alloc] init];
            detail.wedaId = [model.ID stringValue];
            [[AppDelegate sharedAppDelegate] pushViewController:detail];
        }else if ([_type integerValue] == 28){
            CYHomeVideoDetailsViewController *detail = [[CYHomeVideoDetailsViewController alloc] init];
            detail.videoId = [model.ID stringValue];
            [[AppDelegate sharedAppDelegate] pushViewController:detail];
        }
    }else{
        if (self.historyArr.count > 0) {
            if (indexPath.section == 0) {
                _searchText = self.historyArr[indexPath.row];
                _textField.text = self.historyArr[indexPath.row];
                [self search];
            }else{
                _searchText = self.hotArr[indexPath.row];
                _textField.text = self.hotArr[indexPath.row];
                [self search];
            }
        }else{
            _searchText = self.hotArr[indexPath.row];
            _textField.text = self.hotArr[indexPath.row];
            [self search];
        }
    }
}
#pragma mark - 搜索
- (void)search{
    [_textField resignFirstResponder];
    if (_textField.text.length > 0) {
        NSUserDefaults *us = [NSUserDefaults standardUserDefaults];
        if ([us objectForKey:@"searchHistory"]) {
            NSArray *data = [us objectForKey:@"searchHistory"];
            NSMutableArray *muData = [NSMutableArray arrayWithArray:data];
            if (![data containsObject:_textField.text]) {
                [muData insertObject:_textField.text atIndex:0];
            }
            [us setObject:muData forKey:@"searchHistory"];
        }else{
            [us setObject:@[_textField.text] forKey:@"searchHistory"];
        }
        
        [us setObject:_textField.text forKey:@"searchValue"];
        [us synchronize];
        if ([us objectForKey:@"searchHistory"]) {
            NSArray *data = [us objectForKey:@"searchHistory"];
            [self.historyArr removeAllObjects];
            [self.historyArr addObjectsFromArray:data];
        }

    }else{
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }
        return;
    }

    self.page = 1;
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",HOST, @"index"];
    NSDictionary *param = @{@"type":_type, @"keywords":_searchText, @"page":@1};
    __weak typeof(self)weakself = self;
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"搜索 ---- %@",responseResult);
        weakself.isSearch = YES;
        [EasyShowLodingView hidenLoding];
        if ([weakself.tableView.mj_header isRefreshing]) {
            [weakself.tableView.mj_header endRefreshing];
        }
        [weakself.tableHeaderView removeFromSuperview];
        [weakself.view addSubview:weakself.tableHeaderView];
        weakself.tableView.frame = CGRectMake(0, CGRectGetMaxY(weakself.tableHeaderView.frame), kWindowW, kWindowH - Height_NavBar - weakself.tableHeaderView.frame.size.height);
        if ([responseResult objectForKey:@"resError"]) {
            [weakself.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if ([responseResult[@"data"] isKindOfClass:[NSDictionary class]]) {
                    
                    NSDictionary *data = responseResult[@"data"];
                    weakself.count = [data[@"count"] integerValue];
                    NSArray *list = data[@"list"];
                    [weakself.resultArr removeAllObjects];
                    for (NSDictionary *dic in list) {
                        CYProjectModel *model = [CYProjectModel mj_objectWithKeyValues:dic];
                        [weakself.resultArr addObject:model];
                    }
                }
                
//                self.tableView.tableHeaderView = self.tableHeaderView;
            }else{
                [weakself.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
        [weakself.tableView reloadData];
    }];
}

- (void)searchMore{
    if (_textField.text.length == 0) {
        if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
        return;
    }
    self.page ++;
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",HOST, @"index"];
    NSDictionary *param = @{@"type":_type, @"keywords":_searchText, @"page":[NSNumber numberWithInteger:self.page]};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"搜索 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
            self.page --;
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                NSDictionary *data = responseResult[@"data"];
                NSArray *list = data[@"list"];
                for (NSDictionary *dic in list) {
                    CYProjectModel *model = [CYProjectModel mj_objectWithKeyValues:dic];
                    [self.resultArr addObject:model];
                }
//                self.tableView.tableHeaderView = self.tableHeaderView;
                [self.tableView reloadData];
            }else{
                self.page --;
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

#pragma mark - SGTopScrollMenuDelegate
- (void)SGTopScrollMenu:(SGTopScrollMenu *)topScrollMenu didSelectTitleAtIndex:(NSInteger)index{
    if (index == 0) {
        _type = @"3";
    }else if (index == 1){
        _type = @"7";
    }else if (index == 2){
        _type = @"28";
    }else{
        _type = @"4";
    }
    [self search];
}


- (void)getHot{
    self.page = 1;
    NSString *url = [NSString stringWithFormat:@"%@%@",HOST, @"keywords/hot"];
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:nil ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"热门关键词 ---- %@",responseResult);
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if (responseResult[@"data"]) {
                    NSArray *data = responseResult[@"data"];
                    [self.hotArr removeAllObjects];
                    [self.hotArr addObjectsFromArray:data];
                    [self.tableView reloadData];
                }
            }else{
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (_textField.text.length == 0) {
        return NO;
    }
    _searchText = textField.text;
    [self search];
    return YES;
}

- (void)searchAction:(UIButton *)sender{
    if (_textField.text.length == 0) {
        return;
    }
    
    _searchText = _textField.text;
    [self search];
}

#pragma mark - DZNEmptyDataSetDelegate
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"wuneir"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"当前页面没有内容！";
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:RGBOF(0x999999)
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    // 空白页面被点击时开启动画，reloadEmptyDataSet
    [self search];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, kWindowH - Height_NavBar) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BackGroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self search];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self searchMore];
        }];
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        
        [_tableView registerNib:[UINib nibWithNibName:@"CYSearch_HotTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYSearch_HotTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"CYSearch_HistoryTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYSearch_HistoryTableViewCell"];

        [_tableView registerNib:[UINib nibWithNibName:@"CYSearchProjectTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYSearchProjectTableViewCell"];

        [_tableView registerNib:[UINib nibWithNibName:@"CYSearchArticleTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYSearchArticleTableViewCell"];

        [_tableView registerNib:[UINib nibWithNibName:@"CYSearchVideoTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYSearchVideoTableViewCell"];

        [_tableView registerNib:[UINib nibWithNibName:@"CYSearchWenDaTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYSearchWenDaTableViewCell"];

    }
    return _tableView;
}

- (NSMutableArray *)hotArr{
    if (!_hotArr) {
        _hotArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _hotArr;
}

- (NSMutableArray *)historyArr{
    if (!_historyArr) {
        _historyArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _historyArr;
}

- (NSMutableArray *)resultArr{
    if (!_resultArr) {
        _resultArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _resultArr;
}


- (UIView *)titleView{
    if (!_titleView) {
        _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWindowW - 90, 44)];
        
        UITextField *_titleTF = [[UITextField alloc] init];
        _titleTF.frame = CGRectMake(0, 7, _titleView.frame.size.width - 70, 30);
        _titleTF.borderStyle = UITextBorderStyleNone;
        _titleTF.backgroundColor = RGBOF(0xEEEEEE);
        [_titleTF setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
        _titleTF.placeholder = @"搜索";
        _titleTF.layer.cornerRadius = 15;
        _titleTF.textColor = RGBOF(0x434343);
        [_titleTF setFont:[UIFont systemFontOfSize:12]];
        _titleTF.returnKeyType = UIReturnKeySearch;
        UIImageView *userNameLeftViewIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_home"]];
        //图片的显示模式
        userNameLeftViewIV.contentMode= UIViewContentModeCenter;
        //图片的位置和大小
        userNameLeftViewIV.frame= CGRectMake(0,0,30,30);
        //左视图默认是不显示的 设置为始终显示
        _titleTF.leftViewMode= UITextFieldViewModeAlways;
        _titleTF.leftView= userNameLeftViewIV;
        _titleTF.delegate = self;
        _textField = _titleTF;
        [_titleView addSubview:_titleTF];
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(_titleView.frame.size.width - 40, 0, 40, 44)];
        [btn setTitle:@"搜索" forState:UIControlStateNormal];
        [btn setTitleColor:RGBOF(0x141414) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
        [_titleView addSubview:btn];
    }
    return _titleView;
}

- (SGTopScrollMenu *)topMenu{
    if (!_topMenu) {
        _topMenu = [SGTopScrollMenu topScrollMenuWithFrame:CGRectMake(0, 0, kWindowW , 44) NormalColor:RGBOF(0x999999) SelColor:RGBOF(0xF43636)];
        _topMenu.topScrollMenuDelegate = self;
        _topMenu.titlesArr = @[@"项目", @"资讯", @"视频", @"问答"];
    }
    return _topMenu;
}

- (UIView *)tableHeaderView{
    if (!_tableHeaderView) {
        _tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 44)];
        [_tableHeaderView addSubview:self.topMenu];
    }
    return _tableHeaderView;
}

@end
